package org.dtrd.modules.shop.job;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.dtrd.common.util.DateUtils;
import org.dtrd.config.pay.PayConstant;
import org.dtrd.modules.shop.entity.po.DtrdEntShcRdOrder;
import org.dtrd.modules.shop.service.impl.DtrdEntShcRdOrderServiceImpl;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

/**
 * 示例不带参定时任务
 * 
 * @Author Scott
 */
@Slf4j
public class FinishOrderJob implements Job {

	@Autowired
	private DtrdEntShcRdOrderServiceImpl orderService;

	@Override
	public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

		// 订单时间超过15天，订单自动改为已完成
		log.info(String.format(" 发货时间超过15天，订单自动改为已完成:" + DateUtils.getTimestamp()));
		LambdaUpdateWrapper<DtrdEntShcRdOrder> updateWrapper = new LambdaUpdateWrapper<>();
		updateWrapper.lt(DtrdEntShcRdOrder::getCreateTime, DateUtils.dateAdd(-15, new Date()));
		updateWrapper.eq(DtrdEntShcRdOrder::getOrderInfoOrderstatus, PayConstant.OrderStatus.IS_DELIVERY.getType());
		updateWrapper.set(DtrdEntShcRdOrder::getOrderInfoOrderstatus, PayConstant.OrderStatus.COMPLETE.getType());
		updateWrapper.set(DtrdEntShcRdOrder::getCompleteTime, DateUtils.getDate());
		orderService.update(updateWrapper);
	}
}
